package com.cy.lib.image;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;

/**
 * @author CY
 * @title 图片加载器
 * @date 2015/7/20
 * @qq 568305883
 * @email tmxdyf@163.com
 */
public class ImageLoadTask extends AsyncTask<String, Object, Bitmap> {

    ImageLoader mImageLoader;
    String mUri;
    TextView mTextView;
    ImageView mImageView;
    CompoundDrawables mCompoundDrawables;
    ImageSize mImageSize;
    DisplayImageOptions mDisplayImageOptions;

    public enum CompoundDrawables {

        Left,
        Top,
        Right,
        Bottom

    }

    public ImageLoadTask(ImageLoader imageLoader, String uri, TextView tv, CompoundDrawables compoundDrawables) {
        mImageLoader = imageLoader;
        mUri = uri;
        mTextView = tv;
        mCompoundDrawables = compoundDrawables;
    }

    public ImageLoadTask(ImageLoader imageLoader, String uri, ImageView iv) {
        this(imageLoader, uri, iv, null);
    }

    public ImageLoadTask(ImageLoader imageLoader, String uri, ImageView iv, ImageSize imageSize) {
        this(imageLoader, uri, iv, imageSize, null);
    }

    public ImageLoadTask(ImageLoader imageLoader, String uri, ImageView iv, ImageSize imageSize, DisplayImageOptions options) {
        mImageLoader = imageLoader;
        mUri = uri;
        mImageView = iv;
        mImageSize = imageSize;
        mDisplayImageOptions = options;
    }

    public void setImageSize(ImageSize imageSize) {
        mImageSize = imageSize;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        if (TextUtils.isEmpty(mUri)) {
            cancel(true);
        }
        if (mImageSize == null) {
            return mImageLoader.loadImageSync(mUri, mDisplayImageOptions);
        } else {
            return mImageLoader.loadImageSync(mUri, mImageSize, mDisplayImageOptions);
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap == null) {
            return;
        }
        if (mImageView != null) {
            mImageView.setImageBitmap(bitmap);
        } else if (mTextView != null) {
            Drawable drawable = new BitmapDrawable(mTextView.getResources(), bitmap);
            switch (mCompoundDrawables) {
                case Left:
                    mTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                    break;
                case Top:
                    mTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                    break;
                case Right:
                    mTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
                    break;
                case Bottom:
                    mTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
                    break;
            }
        }
    }
}